package com.huluobo.high3demo.home.items;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.databinding.FragmentHomeItemBinding;
import com.huluobo.mvvm.baen.BannerBeanResult;
import com.huluobo.mvvm.baen.GoodsBeanResult;
import com.huluobo.mvvm.view.BaseMVVMFragment;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LC on 2023/9/18.
 */
public class ItemFragment extends BaseMVVMFragment<FragmentHomeItemBinding, ItemViewModel> {

    private int currentPage = 1;
    private int pageSize = 10;
    private ItemAdapter itemAdapter;

    private List<BannerBeanResult.BannerBean> bannerBeanList = new ArrayList<>();

    private List<GoodsBeanResult.GoodsBean> goodsBeans = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_item;
    }

    @Override
    protected void initData() {
        viewModel.loadBanner();
        viewModel.loadGoods(0, currentPage, pageSize);

        viewModel.bannerSuccess.observe(requireActivity(), new Observer<BannerBeanResult>() {
            @Override
            public void onChanged(BannerBeanResult bannerBeanResult) {
                dataBinding.homeItemBanner.setImages(bannerBeanResult.getData());
                dataBinding.homeItemBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置banner的指示器
                dataBinding.homeItemBanner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        Glide.with(requireActivity()).load(path).into(imageView);
                    }
                });
                dataBinding.homeItemBanner.setDelayTime(1500);
                dataBinding.homeItemBanner.setIndicatorGravity(BannerConfig.CENTER);
                dataBinding.homeItemBanner.start();
            }
        });

        viewModel.goodsSuccess.observe(requireActivity(), new Observer<GoodsBeanResult>() {
            @Override
            public void onChanged(GoodsBeanResult goodsBeanResult) {
                if (goodsBeanResult.getData().size() > 0) {
                    goodsBeans.addAll(goodsBeanResult.getData());
                    itemAdapter.notifyDataSetChanged();
                } else {
                    currentPage -= 1;
                }
            }
        });
    }

    @Override
    protected void initView() {
        dataBinding.homeItemRv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        itemAdapter = new ItemAdapter(goodsBeans);
        dataBinding.homeItemRv.setAdapter(itemAdapter);

        dataBinding.llSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPage++;
                viewModel.loadGoods(0, currentPage, pageSize);
            }
        });
    }

    @Override
    protected Class<ItemViewModel> getViewModel() {
        return ItemViewModel.class;
    }
}
